<!--
        Copyright (c) 2023, LXMS (lxinet.cn).

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<template>
  <lx-search-tree
      :url="url.treeUrl"
      :fieldNames="fieldNames"
      :defaultSelect="false"
      @onSelect="getList"
  >
    <template #panel>
      <lx-search-bar :searchConfig="searchConfig" v-model:queryParams="queryParams"
                     @onSearch="handleSearch"></lx-search-bar>
      <div class="containerBox">
        <lx-table
            ref="listTable"
            selectionType="none"
            v-model:selectedRowKeys="selectedRowKeys"
            :rowKey="rowKey"
            :url="url.list"
            :autoLoad="false"
            :loading="loading"
            :ltTools="ltTools"
            :columns="columns"
            :hasPagination="true"
            :params="{...queryParams}"
            :customCell="{style:{'min-width': '1000px'}}"
            @handleToolsClick="handleToolsClick"
        >
          <template #sex="{row}">
            <a-tag v-if="row.sex === 0" color="purple">未知</a-tag>
            <a-tag v-if="row.sex === 1" color="blue">男</a-tag>
            <a-tag v-if="row.sex === 2" color="red">女</a-tag>
          </template>
          <template #status="{row}">
            <a-tag v-if="row.status === 0" color="red">禁用</a-tag>
            <a-tag v-else color="green">启用</a-tag>
          </template>
          <template #action="{row}">
            <a-button @click="handleAction('editNode','edit',row)" size="small" type="link" v-has="'sys:user:update'">
              编辑
            </a-button>
            <a-popconfirm
                title="确定删除吗?"
                @confirm="handleDelete(row)"
            >
              <a-button size="small" type="link" v-has="'sys:user:delete'">删除</a-button>
            </a-popconfirm>
          </template>
        </lx-table>
      </div>
    </template>
  </lx-search-tree>
  <edit ref="editNode"></edit>
</template>

<script>
import lxSearchBar from "@components/baseComponents/lxSearchBar/index.vue"
import lxTable from "@components/baseComponents/lxTable/index.vue"
import edit from "./edit.vue"
import {indexPageMinxin} from "@/minxins/indexPageMinxin"
import lxSearchTree from "@components/baseComponents/lxSearchTree/index.vue";

export default {
  name: "UserIndex",
  components: {
    lxSearchTree,
    lxSearchBar,
    lxTable,
    edit
  },
  mixins: [indexPageMinxin],
  data() {
    return {
      url: {
        list: "/system/user/list",
        delete: "/system/user/delete",
        treeUrl: "/system/dept/simpleList",
        batchDelete: "",
      },
      fieldNames: {
        children: 'children', title: 'name', key: 'id'
      },
      columns: [
        {
          title: '用户名',
          key: 'name',
        },
        {
          title: '姓名',
          key: 'realName',
        }, {
          title: '手机号',
          key: 'phone',
          width: 150
        }, {
          title: '性别',
          key: 'sex',
          columnType: "sex",
          render: "sex"
        }, {
          title: '状态',
          key: 'status',
          columnType: "status",
          render: "status"
        },
        {
          title: '创建时间',
          key: 'createTime',
          width: 180

        },
        {
          title: '操作',
          key: 'action',
          render: 'action',
          width: 120,
        }
      ],
      searchConfig: [
        {
          component: "a-input",
          key: "name",
          title: "名称",
          props: {
            allowClear: true,
          }
        }
      ],
      ltTools: [
        {
          title: "新增",
          key: "add",
          permission: "sys:user:add",
          props: {
            type: "primary"
          }
        }
      ],
    };
  },
  mounted() {
    this.getList();

  },
  methods: {
    getList(item) {
      this.loading = true;
      if (item) {
        this.queryParams.deptId = item[0];
      }
      this.$nextTick(() => {
        this.$refs.listTable.loadData(1);
      })
      this.loading = false;
    },
    handleAdd(refName, key, record) {
      this.$refs[refName].formData = {
        seq: 50
      };
      this.$refs[refName].title = this.$route.meta.title + "- 新增";
      this.$refs[refName].isShow = true;
      this.$refs[refName].action = "add";
    },
  },
}
</script>

<style scoped>

</style>